package util.preprocessing;

import java.util.HashSet;
import java.util.Iterator;


import util.io.FileInput;


/**
 * 
 * @author Sergio
 *
 */
public class StringAnalysis {

	/**
	 * @param args
	 */
	
	/**
	 * Path with the list of English stop words (each word in a line)
	 */
	//static final String englishStopWordsPath = "/home/sergio/projects/data/dict/english_stop_words.txt";
	static final String englishStopWordsPath = "/home/sergio/projects/data/dict/english_stop_words.txt";

	/**
	 * Hashset containing the English stop words given in the englishStopWordsPath file
	 */
	static final HashSet<String> englishStopWords = initStopWords(englishStopWordsPath);

	
	
	/**
	 * 
	 * 
	 * @param path
	 * @return
	 */
	static public HashSet<String> initStopWords(String path) {

		HashSet<String> set = new HashSet<String>();
		FileInput in = new FileInput(path);

		String line = in.readString();

		while (line != null) {

			set.add(line.trim());
			line = in.readString();

		}

		return set;

	}
	
	/**
	 * Normalize an entry of the AOL query log
	 * 
	 * The normalization is carried out by:
	 * 	Deleting stop words
	 *  Deleting www., http:\\,  .com, .net, .nl, file extensions, etc
	 *  Deleting whiteSpaces (only single whitespaces are kept)
	 *  
	 * 
	 * @param query
	 * @return
	 */
	public static String queryEntryNormalization(String query) {

		String n = query;

		n = n.replaceAll("(\\.[a-z]{2,3})+$", "");
		n = n.replaceAll("\\.[a-z]{2,3}\\s+", " ");

		n = n.replaceAll("www\\.|http:\\\\|http://|http://www\\.|ww\\.", "");
		
		n = n.replaceAll("\\.|,|:|;|'|-", " ");
		
		
		n = n.replaceAll("\\s+", " "); // remove extra whiteSpaces

		// The next lines remove stops words from the query

		String query_words[] = n.split("\\s+");
		String new_query = "";
		for (int i = 0; i < query_words.length; i++) {

			if (!englishStopWords.contains(query_words[i].trim())) {
				new_query += query_words[i] + " ";

			}
		}

		// last character contains a whiteSpace
		
		if(new_query.length()==0) return query;
		
		return new_query.substring(0, new_query.length() - 1);

	}
	
	
	


	// ****************************
	// Get minimum of three values
	// ****************************

	private static int Minimum(int a, int b, int c) {
		int mi;

		mi = a;
		if (b < mi) {
			mi = b;
		}
		if (c < mi) {
			mi = c;
		}
		return mi;

	}

	public static int getEditDistance(String s, String t) {
		int d[][]; // matrix
		int n; // length of s
		int m; // length of t
		int i; // iterates through s
		int j; // iterates through t
		char s_i; // ith character of s
		char t_j; // jth character of t
		int cost; // cost

		// Step 1

		n = s.length();
		m = t.length();
		if (n == 0) {
			return m;
		}
		if (m == 0) {
			return n;
		}
		d = new int[n + 1][m + 1];

		// Step 2

		for (i = 0; i <= n; i++) {
			d[i][0] = i;
		}

		for (j = 0; j <= m; j++) {
			d[0][j] = j;
		}

		// Step 3

		for (i = 1; i <= n; i++) {

			s_i = s.charAt(i - 1);

			// Step 4

			for (j = 1; j <= m; j++) {

				t_j = t.charAt(j - 1);

				// Step 5

				if (s_i == t_j) {
					cost = 0;
				} else {
					cost = 1;
				}

				// Step 6

				d[i][j] = Minimum(d[i - 1][j] + 1, d[i][j - 1] + 1,
						d[i - 1][j - 1] + cost);

			}

		}

		// Step 7

		return d[n][m];

	}
	
	
	public static int getWordEditDistance(String s, String t) {
		int d[][]; // matrix
		int n; // length of s
		int m; // length of t
		int i; // iterates through s
		int j; // iterates through t
		String s_i; // ith word of s
		String t_j; // jth word of t
		int cost; // cost

		// Step 1

		String s_word[] = s.trim().split("\\s+");
		String t_word[] = t.trim().split("\\s+");

		n = s_word.length;
		m = t_word.length;

		if (n == 0) {
			return m;
		}
		if (m == 0) {
			return n;
		}
		d = new int[n + 1][m + 1];

		// Step 2

		for (i = 0; i <= n; i++) {
			d[i][0] = i;
		}

		for (j = 0; j <= m; j++) {
			d[0][j] = j;
		}

		// Step 3

		for (i = 1; i <= n; i++) {

			s_i = s_word[i - 1];

			// Step 4

			for (j = 1; j <= m; j++) {

				t_j = t_word[j - 1];

				// Step 5

				if (s_i.equals(t_j)) {
					cost = 0;
				} else {
					cost = 1;
				}

				// Step 6

				d[i][j] = Minimum(d[i - 1][j] + 1, d[i][j - 1] + 1,
						d[i - 1][j - 1] + cost);

			}

		}

		// Step 7

		return d[n][m];

	}
	
	
	
	
	
	public static String hashSetToString(HashSet<String> set) {

		Iterator<String> iter = set.iterator();
		
		String types_concat = iter.next();

		while (iter.hasNext()) {

			types_concat += "\t" + iter.next();
		}

		return types_concat;

	}
	
	
	public static String cleanQueryParser(String string){
		
		
		String a = string.replaceAll("\\?|\\*|" +
				"\"|" +
				"\'|" +
				"'|:|\\(|\\)|[|{|]|}|\\+|@|%|\\^|~|`|!"
				, "");
		
		return a;
	}
	
		
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		String a= "http:\\\\ww.something.cza.nl buenas";
		
		String b= "ww.someth-ing.cza.nl";
		System.out.println(queryEntryNormalization(b));
		
		
		String c= "for~`mat){!}(1)";
		
		System.out.println(cleanQueryParser(c));
		
	}

}
